<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/3
 * Time: 15:29
 */

namespace App\library;


class Tree
{
    public $arr=[];
    public function getInvId($data, $invId)
    {
        foreach ($data as $key => $val) {
            if ($val['pid'] == $invId) {
                $this->arr[] = $val;
                $this->getInvId($data, $val['id']);
            }
        }
        return $this->arr;
    }
    //递归获取所有上级
    public function get_top_parents($data,$invid){
        static $arr=[];
        foreach ($data as $key => $value) {
            if($invid==$value['id']){
                $arr[]=$value;
                $this->get_top_parent($data,$value['pid']);
            }
        }
        return $arr;
    }
//递归获取顶级
    public function get_top_parent($data,$invid){
        static $arr=[];
        foreach ($data as $key => $value) {
            if($invid==$value['id']){
                if($value['pid']!=0){
                    $this->get_top_parent($data,$value['pid']);
                }else{
                    $arr[]=$value;
                }
            }
        }
        return $arr;
    }
//递归打乱数组

    public function shuffle_assoc($list) {
        if (!is_array($list)) return $list;

        $keys = array_keys($list);
        shuffle($keys);
        $random = array();
        foreach ($keys as $key)
            $random[$key] = $this->shuffle_assoc($list[$key]);
        return $random;
    }
}